<?php
class m_pemilihan_adik_asuh extends Model{
    var $table = 'pemilihan_adik_asuh';
    function getCountRows($params){
        $where = "";
        if($params['awal'] != "" && $params['akhir']){
            $where .= "where tanggal between '$params[awal]' and '$params[akhir]'";
        }
        
        $query = $this->db->query("select * from pemilihan_adik_asuh $where");
        return $query->num_rows();
    }
    function getPemilihanAdikAsuh($offset = NULL,$limit = NULL,$params){
        $where = "";
        if($params['awal'] != "" && $params['akhir'] != ""){
            $where .= "where tanggal between '$params[awal]' and '$params[akhir]'";
        }
        
        $query = $this->db->query("select * from pemilihan_adik_asuh $where limit $offset,$limit");
        if($query->num_rows() > 0){
            return $query->result_array();
        }else return array();
    }
    function getPeriode(){
        $query = $this->db->query('select max(periode) as periode from pemilihan_adik_asuh');
        
        return $query->result_array();
    }
    function pemilihanAdikAsuhProses($data){
        $alternatif = $this->session->userdata('alternatif');
        $kriteria = $this->session->userdata('kriteria');
        $bobot = $this->session->userdata('bobot');
        
        foreach ($this->getPeriode() as $period);
        $periode = $period['periode'] + 1;
        
        $dataPemilihan = array('tanggal' => date('Y-m-d'),
            'periode' => $periode);
        $insertDataPemilihan = $this->db->insert($this->table,$dataPemilihan);
        $idPemilihan = $this->db->insert_id();
        
        //Input data bobot preferensi
        $no = 0;
        foreach ($bobot['id'] as $bob){
            $qbobot = "insert into bobot_preferensi (id,id_pemilihan_adik_asuh,id_kriteria,id_tingkat_kepentingan_kriteria) values ('','$idPemilihan','".$kriteria['id'][$no]."','".$bob."')";
            $this->db->query($qbobot);
            $no++;        
        }
        
        //Input data matrik keputusan
        $num = 0;
        foreach ($alternatif['id'] as $alt){
            $nums = 0;
            foreach ($data as $key => $kri){
                $qmatrik = "insert into matrik_keputusan (id,id_pemilihan_adik_asuh,id_calon_adik_asuh,id_kriteria,bobot) values ('','$idPemilihan','$alt','".$kriteria['id'][$nums]."','$kri[$num]')";
                $this->db->query($qmatrik);
                $nums++;
            }
            $num++;
        }
        
        //Menghitung rating kinerja tiap alternatif
        foreach ($data as $key => $kri){
            $pembagi = 0;
            $no = 0;
            foreach ($alternatif['id'] as $alt){
                $pembagi = $pembagi + pow($kri[$no], 2);
                $no++;
            }
            $xTernormalisasi[] = sqrt($pembagi);
        }
        
        //Menghitung matrik ternormalisasi terbobot
        $no = 0;
        foreach ($alternatif['id'] as $alt){
            $num = 0;
            foreach ($data as $key => $kri){
               $terbobot[$num][$no] = ($kri[$no]/$xTernormalisasi[$num])*$bobot['nilai_bobot'][$num]; 
               $num++; 
            }
            $no++;
        }
        
        //Menghitung solusi ideal positif
        $no = 0;
        foreach ($kriteria['id'] as $kri){
            if($kriteria['jenis'][$no] == 'Cost'){
                $positif[$no] = min($terbobot[$no]);
            }else{
                $positif[$no] = max($terbobot[$no]);
            }
          $no++;  
        }
        
        //Menghitung solusi ideal negatif
        $no = 0;
        foreach ($kriteria['id'] as $kri){
            if($kriteria['jenis'][$no] == 'Cost'){
                $negatif[$no] = max($terbobot[$no]);
            }else{
                $negatif[$no] = min($terbobot[$no]);
            }
          $no++;  
        }
        
        //Menghitung jarak nilai terbobot dengan solusi ideal positif
        $no = 0;
        foreach ($alternatif['id'] as $alt){
           $num = 0; 
           $dtemp = 0;
           $temp = 0;
           foreach ($terbobot as $kri){
             $temp = pow(($kri[$no]-$positif[$num]),2);
             $dtemp = $dtemp + $temp;
             $num++;  
           } 
           $dpositif[$no] = sqrt($dtemp);
           $no++; 
        }
        
        //Menghitung jarak nilai terbobot dengan solusi ideal negatif
        $no = 0;
        foreach ($alternatif['id'] as $alt){
           $num = 0; 
           $dtemp = 0;
           $temp = 0;
           foreach ($terbobot as $kri){
             $temp = pow(($kri[$no]-$negatif[$num]),2);
             $dtemp = $dtemp + $temp;
             $num++;  
           } 
           $dnegatif[$no] = sqrt($dtemp);
           $no++; 
        }
        
        //Menghitung kedekatan setiap alternatif dengan solusi ideal
        $no = 0;
        foreach ($dpositif as $dpos){
           $v[$no] = $dnegatif[$no]/($dpos+$dnegatif[$no]);
           $no++;
        }
        
        //Input data hasil pemilihan adik asuh
        $num = 0;
        foreach ($alternatif['id'] as $alt){
            $qhasil = "insert into hasil_pemilihan_adik_asuh (id,id_pemilihan_adik_asuh,id_calon_adik_asuh,nilai) value ('','$idPemilihan','$alt','$v[$num]')";
            $this->db->query($qhasil);
            $num++;
        }
        return $idPemilihan;
    }
    function getAlternatif($idPemilihan){
        $qcalonAdikAsuh = $this->db->query("select distinct mk.id_calon_adik_asuh, ca.nama as calon_adik_asuh from matrik_keputusan mk join adik_asuh ca on mk.id_calon_adik_asuh = ca.id where mk.id_pemilihan_adik_asuh = '$idPemilihan'");
        if($qcalonAdikAsuh->num_rows() > 0){
            $calonAdikAsuh = $qcalonAdikAsuh->result_array();             
        }else $calonAdikAsuh = array();
        
        return $calonAdikAsuh;
    }
    function getKriteria($idPemilihan){
        $qkriteria = $this->db->query("select distinct mk.id_kriteria,k.jenis from matrik_keputusan mk join kriteria k on mk.id_kriteria = k.id where mk.id_pemilihan_adik_asuh = '$idPemilihan'");
        if($qkriteria->num_rows() > 0){
           $kriteria = $qkriteria->result_array(); 
        }else $kriteria = array();
        
        return $kriteria;
    }
    function getBobot($idPemilihan){
        $qbobot = $this->db->query("select b.id_tingkat_kepentingan_kriteria as id,tk.nilai from bobot_preferensi b join tingkat_kepentingan_kriteria tk on b.id_tingkat_kepentingan_kriteria = tk.id where b.id_pemilihan_adik_asuh = '$idPemilihan' order by b.id");
        if($qbobot->num_rows() > 0){
            $bobot = $qbobot->result_array();
        }else $bobot = array();
        
        return $bobot;
    }
    function getMatrikKeputusan($idPemilihan){
        $calonAdikAsuh = $this->getAlternatif($idPemilihan);
        $kriteria = $this->getKriteria($idPemilihan);
        $no = 0;
        foreach ($calonAdikAsuh as $calon){
            $num = 0;
            foreach ($kriteria as $kri){
                $matrik = $this->db->query("select bobot from matrik_keputusan where id_calon_adik_asuh = '$calon[id_calon_adik_asuh]' and id_kriteria = '$kri[id_kriteria]'");
                foreach ($matrik->result_array() as $nilai);
                $matriks[$no][$num] = $nilai['bobot'];
                $num++;
            }
            $no++;
        }
        return $matriks; 
    }
    function getListHasilPemilihan($idPemilihan){
        $queryMaster = $this->db->query("select * from pemilihan_adik_asuh where id = '$idPemilihan'");
        if($queryMaster->num_rows() > 0){
            $result['master'] = $queryMaster->result_array();
        }else $result['master'] = array();
        
        $queryDetail = $this->db->query("select ca.id as id_calon_adik_asuh,ca.nama as calon_adik_asuh,ca.status,hp.nilai,s.nama as sekolah,k.nama as kelas,tp.nama as tingkat_pendidikan,ta.nama as tahun_ajaran,sms.nama as semester 
                                        from hasil_pemilihan_adik_asuh hp 
                                        join adik_asuh ca on hp.id_calon_adik_asuh = ca.id 
                                        join dinamis_adik_asuh da on da.id_adik_asuh = ca.id
                                        join sekolah s on da.id_sekolah = s.id
                                        join kelas k on da.id_kelas = k.id
                                        join tingkat_pendidikan tp on k.id_tingkat_pendidikan = tp.id
                                        join tahun_ajaran ta on da.id_tahun_ajaran = ta.id
                                        join semester sms on ta.id_semester = sms.id
                                        where hp.id_pemilihan_adik_asuh = '$idPemilihan' and da.status = '1' order by hp.nilai desc");
        if($queryDetail->num_rows() > 0){
            $result['detail'] = $queryDetail->result_array();
        }else $result['detail'] = array();
        
        return $result;
    }
    function pilih_adik_asuh($id_calon_adik_asuh){
        $update = $this->db->query("update adik_asuh set tgl_diterima = now(),status = '1' where id = '$id_calon_adik_asuh'");
        if($update){
            $return = TRUE;
        }else $return = FALSE;
        return $return;
    }
}
?>
